import subprocess
import sys
import os

def build_exe():
    """简单的打包脚本"""
    
    # 检查PyInstaller是否可用
    try:
        import PyInstaller
        print(f"PyInstaller版本: {PyInstaller.__version__}")
    except ImportError:
        print("正在安装PyInstaller...")
        subprocess.run([sys.executable, "-m", "pip", "install", "pyinstaller"])
    
    # 打包命令
    cmd = [
        sys.executable, "-m", "PyInstaller",
        "--onefile",
        "--windowed", 
        "--name=CartoonViewer",
        "--clean",
        "--noconfirm",
        "main.py"
    ]
    
    # 如果图标文件存在，添加图标
    if os.path.exists("icon.ico"):
        cmd.extend(["--icon=icon.ico"])
    
    print("开始打包...")
    print(f"执行命令: {' '.join(cmd)}")
    
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    if result.returncode == 0:
        print("打包成功！")
        if os.path.exists("dist/CartoonViewer.exe"):
            print("可执行文件已生成: dist/CartoonViewer.exe")
        else:
            print("警告: 未找到生成的exe文件")
    else:
        print("打包失败:")
        print(result.stderr)
    
    return result.returncode == 0

if __name__ == "__main__":
    build_exe()